smarthome 是服务器端代码(nodejs),TrunOnMonitorDemo是客户端代码(c#)首先运行 smarthome项目,输入命令 node server.js 即可,如下:
然后打开浏览器 http://localhost:3000 即可看到如下截图:
之后运行 vs项目中的 TrunOnMonitorDemo.exe 即可看到 如下效果
此时刷新 http://localhost:3000 之后看到如下网址,按照下图操作 即可 远程 点亮显示器/关闭显示器
using Quobject.SocketIoClientDotNet.Client;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace TrunOnMonitorDemo{ public partial class Form1 : Form { Socket socket; public Form1() { InitializeComponent(); socket = IO.Socket("http://localhost:3000"); socket.On(Socket.EVENT_CONNECT, () => { socket.Emit("hello come from window"); }); socket.On("onTurnOnScreen", (data) => { MonitorHelper.PowerOn(); }); socket.On("onTurnOffScreen", (data) => { this.BeginInvoke(new Action(() => { MonitorHelper.PowerOff(this.Handle); })); }); } private void btnTurnOff_Click(object sender, EventArgs e) { //MessageBox.Show("即将关闭显示器,5秒钟后又会自动点亮显示器"); MonitorHelper.PowerOff(this.Handle); //方法一:同步方式 //Thread.Sleep(5000); //MonitorHelper.PowerOn(); //方法二:异步方式 (new Thread(() => { Thread.Sleep(5000); MonitorHelper.PowerOn(); })).Start(); } private void btnTurnOn_Click(object sender, EventArgs e) { MonitorHelper.PowerOn(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if (socket != null) socket.Disconnect(); } }}
评论